package com.ys.peony.interfaces;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.ys.peony.R;
import com.ys.peony.ui.activity.BusiLoanInfoActivity;

import java.util.List;

/**
 * tab标签界面控制
 */
public class RadioGroupChangeListener implements OnCheckedChangeListener {

	private BusiLoanInfoActivity mcontext;
	private RadioGroup mrg;

	private List<Fragment> fragments; // 一个tab页面对应一个Fragment
	private int fragmentContentId; // Activity中所要被替换的区域的id

	private int currentTab; // 当前Tab页面索引

    private ImageButton addConversationBtn;

	public RadioGroupChangeListener(BusiLoanInfoActivity mcontext, List<Fragment> fragments, int fragmentContentId, RadioGroup rg) {
		this.mcontext = mcontext;
		this.mrg = rg;
		this.fragments = fragments;
        this.fragmentContentId = fragmentContentId;
        
     // 默认显示第一页
        FragmentTransaction ft =  mcontext.getSupportFragmentManager().beginTransaction();
        ft.add(fragmentContentId, fragments.get(0));
        ft.add(fragmentContentId, fragments.get(1));
        ft.commit();
        ft.hide(fragments.get(1));
        mrg.setOnCheckedChangeListener(this);

	}

	
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		for(int i = 0; i < mrg.getChildCount(); i++){
            if(mrg.getChildAt(i).getId() == checkedId){
                Fragment fragment = fragments.get(i);
                FragmentTransaction ft = obtainFragmentTransaction(i);

                getCurrentFragment().onPause(); // 暂停当前tab

                if(fragment.isAdded()){
                    fragment.onResume(); // 启动目标tab的onResume()
                }else{
                    ft.add(fragmentContentId, fragment);
                }

                showTab(i); // 显示目标tab
                ft.commit();

            }
        }
	}
	/**
     * 切换tab
     * @param idx
     */
    private void showTab(int idx){
        for(int i = 0; i < fragments.size(); i++){
            Fragment fragment = fragments.get(i);
            FragmentTransaction ft = obtainFragmentTransaction(idx);

            if(idx == i){
                ft.show(fragment);
            }else{
                ft.hide(fragment);
            }
            ft.commit();
        }
        currentTab = idx; // 更新目标tab为当前tab
    }

    /**
     * 获取一个带动画的FragmentTransaction
     * @param index
     * @return
     */
    private FragmentTransaction obtainFragmentTransaction(int index){
        FragmentTransaction ft = mcontext.getSupportFragmentManager().beginTransaction();
        // 设置切换动画
        if(index > currentTab){
            ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
        }else{
            ft.setCustomAnimations(R.anim.push_right_in, R.anim.push_right_out);
        }
        return ft;
    }

    public int getCurrentTab() {
        return currentTab;
    }

    public Fragment getCurrentFragment(){
        return fragments.get(currentTab);
    }

}
